package com.meow.action.admin.basic;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.meow.dao.entity.basic.Users;
import com.meow.dao.mapper.basic.UsersDao;
import com.meow.util.AjaxOut;
import com.meow.util.EntityReflect;
import com.meow.util.ExtLimit;

@Controller
@RequestMapping("/admin/basic/UsersAction/")
public class UsersAction {
	private static final long serialVersionUID = -7514105194672793710L;
	private Logger logger = Logger.getLogger(UsersAction.class);
	
	@Autowired
	private UsersDao usersDao;
	
	/**
	 * 查询
	 */
	@RequestMapping("search.action")
	public void search(HttpServletRequest request, HttpServletResponse response) {
		try {
			Users userEty = (Users) EntityReflect.createObjectFromRequest(request, Users.class);
			ExtLimit limit = (ExtLimit) EntityReflect.createObjectFromRequest(request, ExtLimit.class);
			userEty.setExtLimit(limit);
			int count = usersDao.selectLimitCount(userEty);
			List list = usersDao.selectByLimit(userEty);
			AjaxOut.responseJSonGrid(response, list, count, new SimpleDateFormat("yyyy-MM-dd"));
		}
		catch (Exception e) {
			logger.error("", e);
		}
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("save.action")
	public void save(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		try {
			Users userEty = (Users) EntityReflect.createObjectFromRequest(request, Users.class);
			if(userEty.getId() == null) {
				userEty.setCreateTime(new Date());
				usersDao.insert(userEty);
			}
			else {
				usersDao.updateById(userEty);
			}
			obj.put("result","success");
		}
		catch (Exception e) {
			logger.error("", e);
			obj.put("result","error");
			obj.put("result",e.getMessage());
		}
		AjaxOut.responseText(response, obj.toString());
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("delete.action")
	public void delete(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		try {
			String userId = request.getParameter("userId");
			usersDao.deleteById(Integer.parseInt(userId));
			obj.put("result","success");
		}
		catch (Exception e) {
			logger.error("", e);
			obj.put("result","error");
			obj.put("info",e.getMessage());
		}
		AjaxOut.responseText(response, obj.toString());
	}
	
	/**
	 * 查看详细
	 */
	@RequestMapping("getDetailInfo.action")
	public void getDetailInfo(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		try {
			String userId = request.getParameter("userId");
			Users userEty = (Users) usersDao.selectById(Integer.parseInt(userId));
			obj.put("data", userEty);
		}
		catch (Exception e) {
			logger.error("", e);
			obj.put("result","error");
			obj.put("result",e.getMessage());
		}
		AjaxOut.responseText(response, obj.toString());
	}

}
